using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbKhachHangDAL
    {
        private SqlConnection cn = null;
        public tbKhachHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbKhachHangDAL(tbKhachHangDAL khachhang)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbKhachHang khachhang)
        {
            SqlCommand cm = new SqlCommand("sp_tbKhachHang_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@HoTen", khachhang.HoTen);
            cm.Parameters.AddWithValue("@GioiTinh", khachhang.GioiTinh);
            cm.Parameters.AddWithValue("@NgaySinh", khachhang.NgaySinh);
            cm.Parameters.AddWithValue("@Email", khachhang.Email);
            cm.Parameters.AddWithValue("@TaiKhoan", khachhang.TaiKhoan);
            cm.Parameters.AddWithValue("@MatKhau", khachhang.MatKhau);
            cm.Parameters.AddWithValue("@DiaChi", khachhang.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", khachhang.DienThoai);
            cm.Parameters.AddWithValue("@SoTKNH", khachhang.SoTKNH);
            cm.ExecuteNonQuery();
        }
        public void Update(tbKhachHang khachhang)
        {
            SqlCommand cm = new SqlCommand("sp_tbKhachHang_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaKH", khachhang.MaKH);
            cm.Parameters.AddWithValue("@HoTen", khachhang.HoTen);
            cm.Parameters.AddWithValue("@GioiTinh", khachhang.GioiTinh);
            cm.Parameters.AddWithValue("@NgaySinh", khachhang.NgaySinh);
            cm.Parameters.AddWithValue("@Email", khachhang.Email);
            cm.Parameters.AddWithValue("@TaiKhoan", khachhang.TaiKhoan);
            cm.Parameters.AddWithValue("@MatKhau", khachhang.MatKhau);
            cm.Parameters.AddWithValue("@DiaChi", khachhang.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", khachhang.DienThoai);
            cm.Parameters.AddWithValue("@SoTKNH", khachhang.SoTKNH);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbKhachHang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaKH", iD);
            cm.ExecuteNonQuery();
        }
        public tbKhachHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbKhachHang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbKhachHangCollection khCollection = new tbKhachHangCollection();
            while (dr.Read())
            {
                tbKhachHang khachhang = new tbKhachHang();
                khachhang.MaKH = int.Parse(dr.GetValue(0).ToString());
                khachhang.HoTen = dr.GetValue(1).ToString();
                khachhang.GioiTinh = dr.GetValue(2).ToString();
                khachhang.NgaySinh =DateTime .Parse ( dr.GetValue(3).ToString());
                khachhang.Email = dr.GetValue(4).ToString();
                khachhang.TaiKhoan = dr.GetValue(5).ToString();
                khachhang.MatKhau = dr.GetValue(6).ToString();
                khachhang.DiaChi = dr.GetValue(7).ToString();
                khachhang.DienThoai = dr.GetValue(8).ToString();
                khachhang.SoTKNH = dr.GetValue(9).ToString();
                khCollection.Add(khachhang);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return khCollection;
        }
        public Boolean KiemTraDangNhap(string user, string pass)
        {
            string sql = "select count(*) from tbKhachHang where TaiKhoan='" + user + "' and MatKhau='" + pass + "'";
            SqlCommand cmd = new SqlCommand(sql, cn);
            int kq = (int)cmd.ExecuteScalar();
            if (kq > 0)
                return true;
            else
                return false;
        }
        public Boolean KiemTraKhachHang(string user)
        {
            string sql = "select count(*) from tbKhachHang where Email='" + user + "'";
            SqlCommand cmd = new SqlCommand(sql, cn);
            int kq = (int)cmd.ExecuteScalar();
            if (kq > 0)
                return true;
            else
                return false;
        }
    }
}